# -*- coding: utf-8 -*-
# @Date   : 2021/07/19
# @File   : R77_EG_OperatingYearsCheck_CF.py
# @Author : zhanghengfeng

'''eof
name:申请企业经营年限校验
code:R77_EG_OperatingYearsCheck_CF
tableName:
columnName:
groups:出口信息模块
dependencies:CF_CQ_DSJ
type:常用指标
dataSourceType:
eof'''

import datetime

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def furthestTime_method():
    '''
    Explanation
    --------------
    获取出口货物金额列表中货物贸易金额大于0且距今时间最远的时间节点
        
    Return
    ------
    monthFinal: int or str即u'缺失值'
        最远时间年月份
    '''
    #获取receipt表失败时，返回缺失值
    try:
        receipt = CF_CQ_DSJ['body']['baseInfo'][0]['receipt']
        if receipt in null_type_list or receipt == []:
            return u'缺失值'
        elif isinstance(receipt,list) and len(receipt)>0:
            #遍历所有的申报记录，对相应月份进行字段值更新
            errorConts = 0
            monthFinal = (datetime.datetime.now()).strftime('%Y%m%d')[:6]
            for i in receipt:
                try:
                    #月份
                    month = i.get('month')
                    #金额
                    goods = i.get('goods')
                    if isfloat(month) and isfloat(goods) and isinstance(int(month),int) and len(month) == 6:
                        if int(month) <  int(monthFinal) and float(goods) > 0:
                            monthFinal = month
                        else:
                            pass
                    else:
                        errorConts += 1
                except:
                    errorConts += 1
            if errorConts == len(receipt):
                return u'缺失值' 
            else:
                return monthFinal
        else:
            return u'缺失值' 
    except:
        return u'缺失值' 
            

def R77_EG_OperatingYearsCheck_CF():
    furthestTime = furthestTime_method()
    if furthestTime == u'缺失值':
        return u'缺失值'
    else:
        nowTime = datetime.datetime.now().year*12 + datetime.datetime.now().month
        furthestTime = int(furthestTime[0:4])*12 + int(furthestTime[4:6])
        if nowTime - furthestTime > 24:
            return 1
        else:
            return 0

result = R77_EG_OperatingYearsCheck_CF()
    